package com.xxh.dataaccess.service.impl;

import com.xxh.dataaccess.dao.ProudDao;
import com.xxh.dataaccess.dao.VideoDao;
import com.xxh.dataaccess.entity.Proud;
import com.xxh.dataaccess.entity.Video;
import com.xxh.dataaccess.service.ProudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("ProudService")
public class ProudServiceImpl implements ProudService {

    @Autowired
    ProudDao proudDao;

    @Autowired
    VideoDao videoDao;

    public boolean isProudVideo(String bv, Long uid) {
        Proud proud = proudDao.findByUid(uid);
        if (null == proud) {
            return false;
        }
        if (proud.getBvs().contains(bv)) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public void setProudVideos(String bv, Long uid) {
        Proud proud = proudDao.findByUid(uid);
        if (null == proud) {
            Proud pr = new Proud();
            pr.setUid(uid);
            ArrayList<String> list = new ArrayList<>();
            list.add(bv);
            pr.setBvs(list);
            proudDao.insert(pr);
        } else {
            List<String> bvs = proud.getBvs();
            if (bvs.size() <= 3) {
                bvs.add(bv);
                proud.setBvs(bvs);
                proudDao.save(proud);
            }
        }
    }

    public void removeProudVideos(String bv, Long uid) {
        Proud proud = proudDao.findByUid(uid);
        if (null == proud) {

        } else {
            List<String> bvs = proud.getBvs();
            bvs.removeIf(s -> s.equals(bv));
            proud.setBvs(bvs);
            proudDao.save(proud);
        }
    }

    @Override
    public List<Video> getProudVideos(Long uid) {
        Proud proud = proudDao.findByUid(uid);
        if (null != proud) {
            List<Video> list = videoDao.findByBvIn(proud.getBvs());
            return list;
        }
        return new ArrayList<Video>();
    }
}
